package com.lyxmaster.lib.view;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.lyxmaster.lib.bluetooth.BtHelper;
import com.lyxmaster.lib.hud.KProgressHUD;
import com.lyxmaster.lib.toasty.Toasty;

import java.io.IOException;

/**
 * Created by LYX on 2018/3/27.
 */

public class BaseActivity extends AppCompatActivity {
    public boolean bFinishTwice = false;    //是否开始按两次返回键退出

    public KProgressHUD hud;
    public Context context;
    public Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init(){
        getPermission();
        context = this;
        activity = this;

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        hud = KProgressHUD.create(context);
        hud.setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setCancellable(false)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f);
    }

    @Override
    public void onDestroy(){
        super.onDestroy();

        hud.dismiss();
    }

    // 用来计算返回键的点击间隔时间
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if(!bFinishTwice)  return super.onKeyDown(keyCode, event);
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                //弹出提示，可以有多种方式
//                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                Toasty.warning(context, "再按一次退出").show();
                exitTime = System.currentTimeMillis();
            } else {
                try {
                    BtHelper.getInstance().onDisconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                finish();

            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    /**
     * 6.0以上系统获取权限
     */
    private void getPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String[] pers = new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE
//                    Manifest.permission.ACCESS_COARSE_LOCATION
//                    Manifest.permission.CAMERA,
//                    Manifest.permission.READ_EXTERNAL_STORAGE,
//                    Manifest.permission.VIBRATE,
//                    Manifest.permission.WAKE_LOCK
            };
            int permissionCheck = 0;
            for(String temp : pers){
                permissionCheck += this.checkSelfPermission(temp);
            }
            if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                this.requestPermissions( // 请求授权
                        pers,0);// 自定义常量,任意整型
            } else {
                Log.d("li", "onRequestPermissionsResult: OK");
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 0:
                if (hasAllPermissionGranted(grantResults)) {
                    Log.d("li", "onRequestPermissionsResult: OK");
                } else {
                    finish();
                }
                break;
        }
    }

    private boolean hasAllPermissionGranted(int[] grantResults) {
        for (int grantResult : grantResults) {
            if (grantResult == PackageManager.PERMISSION_DENIED) {
                return false;
            }
        }
        return true;
    }
}
